package com.controller.developerPortal;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/upload/")
public class UploadFileController {
	
		   		    @RequestMapping(value = "/", method = RequestMethod.GET)
		    public String index() {
		        return "uploadFile";
		   		    }
		   		    
		   		    
		   		 @RequestMapping(value = "/", method = RequestMethod.POST)
		   	    public String save(@RequestParam List<MultipartFile> files, Model model) {
		   	        model.addAttribute("files", files);	
		   	        MultipartFile file = files.get(0);
		   	        String fileName = file.getOriginalFilename();
		   	        if (!file.isEmpty()){
						try {
							byte[] bytes = file.getBytes();
							File serverFile = new File("C:\\SaveYourFileHere\\" + fileName);
							BufferedOutputStream stream = new BufferedOutputStream(
									new FileOutputStream(serverFile));
							stream.write(bytes);
							stream.close();
							
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		   		 }
		   	        return "uploadFile";
		   		}
}
